package gvjclient.actions;

import gvjclient.GVJclient;
import gvjclient.ui.GVJclientUI;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.techventus.server.voice.datatypes.Phone;

public class CallAction extends AbstractAction {

	private Phone sourcePhone;
	private String destinationNumber;
	private GVJclient client;
	private JComboBox accountCombo = null;
	private JComboBox sourceCombo = null;

	public CallAction(GVJclient client, Phone sourcePhone, String destinationNumber)
	{
		super("Call with " + sourcePhone.getPhoneNumber());
		this.sourcePhone = sourcePhone;
		this.destinationNumber = destinationNumber;
		this.client = client;
	}
	
	public CallAction(GVJclient client, Phone sourcePhone)
	{
		super("Place call with " + sourcePhone.getPhoneNumber());
		this.sourcePhone = sourcePhone;
		this.client = client;
	}
	
	public CallAction()
	{
		super("Call");
		ImageIcon imageIcon = new ImageIcon(getClass().getResource(File.separator + "images" + File.separator + "call.png"));
		this.putValue(SHORT_DESCRIPTION, "Place Call");
		this.putValue(SMALL_ICON, imageIcon);
	}
	
	@Override
	public void actionPerformed(ActionEvent e)
	{
			JTextField destinationText;
			JLabel sourceLabel;
			JLabel destinationLabel;
			JLabel accountLabel;
			JPanel panel;
			GVJclient[] clients;
			JLabel msgLabel;
			int result;
			int i;
			ActionListener accountActionListener = null;
			ActionListener sourceActionListener = null;
			KeyListener keyListener = null;
			Dimension preferredSize = new Dimension(175,25);

			panel = new JPanel();
			panel.setLayout(new FlowLayout());
			msgLabel = new JLabel("Press OK to place call.");
			sourceLabel = new JLabel("Call With");
			destinationLabel = new JLabel("Destination");
			accountLabel = new JLabel("Account");
			accountCombo = new JComboBox();
			sourceCombo = new JComboBox();
			
			accountActionListener = new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					Object[] clients;
					JComboBox source = (JComboBox)e.getSource();
					clients = source.getSelectedObjects();
					if(clients.length>0)
					{
						client = (GVJclient)clients[0];
						populateSourceCombo();
					}
				}
			};
			
			sourceActionListener = new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e)
				{
					Object[] phones;
					JComboBox source = (JComboBox)e.getSource();
					phones = source.getSelectedObjects();
					if(phones.length>0)
						sourcePhone = (Phone)phones[0];
				}
			};
			
			keyListener = new DestinationNumberKeyListener();
	
			if(this.client != null)
			{
				accountCombo.addItem(this.client);
				accountCombo.setEnabled(false);
				this.accountCombo.addActionListener(accountActionListener);
				if(this.sourcePhone != null)
				{
					sourceCombo.addItem(this.sourcePhone);
					sourceCombo.setEnabled(false);
					this.sourceCombo.addActionListener(sourceActionListener);
				}
				else
				{
					this.populateSourceCombo();
				}
				this.sourceCombo.addActionListener(sourceActionListener);
			}
			else //this.client == null
			{
				clients = GVJclientUI.get_instance().getLoggedInClients();
				this.accountCombo.removeAllItems();
				if(clients.length>0)
				{
					for(i=0;i<clients.length;i++)
					{
						accountCombo.addItem(clients[i]);
					}
					this.accountCombo.addActionListener(accountActionListener);
					accountCombo.setSelectedItem(clients[0]);
					this.sourceCombo.addActionListener(sourceActionListener);
				}
			}

			destinationText = new JTextField();
			if ( this.destinationNumber != null)
			{
				destinationText.setText(destinationNumber);
				destinationText.setEnabled(false);
			}
			else 
			{
				destinationText.addKeyListener(keyListener);
			}
			panel.add(accountLabel);
			panel.add(accountCombo);
			panel.add(sourceLabel);
			panel.add(sourceCombo);
			panel.add(destinationLabel);
			panel.add(destinationText);
			panel.add(msgLabel);
			panel.setPreferredSize(new Dimension(400,175));
			accountLabel.setPreferredSize(preferredSize);
			accountCombo.setPreferredSize(preferredSize);
			sourceLabel.setPreferredSize(preferredSize);
			sourceCombo.setPreferredSize(preferredSize);
			destinationLabel.setPreferredSize(preferredSize);
			destinationText.setPreferredSize(preferredSize);
			msgLabel.setPreferredSize(new Dimension(350,25));
			destinationText.addHierarchyListener(new HierarchyListener() {
		        public void hierarchyChanged(HierarchyEvent e) {
		            final Component c = e.getComponent();
		            if (c.isShowing() && (e.getChangeFlags() &
		                HierarchyEvent.SHOWING_CHANGED) != 0) {
		                Window toplevel = SwingUtilities.getWindowAncestor(c);
		                toplevel.addWindowFocusListener(new WindowAdapter() {
		                    public void windowGainedFocus(WindowEvent e) {
		                        c.requestFocus();
		                    }
		                });
		            }
		        }
		    });
			result = JOptionPane.showConfirmDialog(GVJclientUI.get_instance(),
					panel,
					"Place Call",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
			if ( result == JOptionPane.OK_OPTION)
			{
				try 
				{
					this.destinationNumber = destinationText.getText();
					this.sourcePhone = (Phone)this.sourceCombo.getSelectedObjects()[0];
					this.client.call(this.sourcePhone.getPhoneNumber(),
								this.destinationNumber, String.valueOf(this.sourcePhone.getType()));
				} catch (IOException ex)
				{
					System.out.println("Error placing call");
				}
			}
			this.destinationNumber = null;
			this.sourcePhone = null;
			this.client = null;
			this.accountCombo = null;
			this.sourceCombo = null;
		}

	private void populateSourceCombo()
	{
		int i;
		Phone[] phones = this.client.getPhones();
		
		this.sourceCombo.removeAllItems();
		if(phones.length>0)
		{
			for (i=0;i<phones.length;i++)
			{
				this.sourceCombo.addItem(phones[i]);
			}
			this.sourceCombo.setSelectedItem(phones[0]);
		}
	}
}